package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.me.SetPwdSuccessFragment;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

import static com.blankj.utilcode.util.StringUtils.getString;


public class SetPwdViewModel extends BaseViewModel {
    //原密码的绑定
    public ObservableField<String> passwordOld = new ObservableField<>("");
    //新密码的绑定
    public ObservableField<String> passwordNew = new ObservableField<>("");
    //新密码的绑定1
    public ObservableField<String> passwordNew1 = new ObservableField<>("");
    public ObservableField<Integer> passwordOldVisible = new ObservableField<>(View.GONE);

    public ObservableInt pwdBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt pwdBtn1Visibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt pwdBtn2Visibility = new ObservableInt(View.INVISIBLE);

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> p1SwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> p2SwitchEvent = new SingleLiveEvent<>();
    }
    public SetPwdViewModel(@NonNull Application application) {
        super(application);
    }

    //提交按钮的点击事件
    public BindingCommand confirmOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            requestData();
        }
    });

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });
    public BindingCommand<Boolean> onPwdFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                pwdBtnVisibility.set(View.VISIBLE);
            } else {
                pwdBtnVisibility.set(View.INVISIBLE);
            }
        }
    });

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand password2ShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.p2SwitchEvent.setValue(uc.p2SwitchEvent.getValue() == null || !uc.p2SwitchEvent.getValue());
        }
    });
    public BindingCommand<Boolean> onPwd2FocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                pwdBtn2Visibility.set(View.VISIBLE);
            } else {
                pwdBtn2Visibility.set(View.INVISIBLE);
            }
        }
    });

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand password1ShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.p1SwitchEvent.setValue(uc.p1SwitchEvent.getValue() == null || !uc.p1SwitchEvent.getValue());
        }
    });
    public BindingCommand<Boolean> onPwd1FocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                pwdBtn1Visibility.set(View.VISIBLE);
            } else {
                pwdBtn1Visibility.set(View.INVISIBLE);
            }
        }
    });




    public void setData(Bundle bundle) {
        if (bundle != null) {
            passwordOldVisible.set(bundle.getBoolean("hasPassword") ? View.VISIBLE : View.GONE);
        }
    }

    public void requestData() {
        if (passwordOldVisible.get() == View.VISIBLE) {
            if (TextUtils.isEmpty(passwordOld.get())) {
                showWarnTip(getApplication().getString(R.string.enter_old_pwd));
                return;
            }
        }
        if (TextUtils.isEmpty(passwordNew.get())) {
            showWarnTip(getApplication().getString(R.string.edit_new_password_hint));
            return;
        }
        if(passwordNew.get().length()<6&&passwordNew.get().length()>20){
            showWarnTip(getApplication().getString(R.string.edit_account_password_hint));
            return;
        }
        if (TextUtils.isEmpty(passwordNew1.get())) {
            showWarnTip(getApplication().getString(R.string.repeat_new_password_hint));
            return;
        }
        if (!passwordNew.get().equals(passwordNew1.get())) {
            showWarnTip(getApplication().getString(R.string.pwd_not_match));
            return;
        }
        showProgress();
        TelegramManager.getTelegramManager().alterLoginPassword(getApplication()
                , passwordOldVisible.get() == View.VISIBLE ? passwordOld.get() : null
                , passwordNew.get(), new TgResultBlock<TgResponse>() {
                    @Override
                    public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                        hideProgress();
                        if (obj.ok) {
                            Bundle bundle = new Bundle();
                            bundle.putString("success", getString(R.string.set_new_pwd_success));
                            startContainerActivity(SetPwdSuccessFragment.class.getCanonicalName(), bundle);
                            finish();
                        }else {
                            showWarnTip(obj.errString);
                        }
                    }

                    @Override
                    public void onTimeout(Map<String, Object> request) {
                        hideProgress();
                        showErrorTip(getString(R.string.request_error));
                    }
                });
    }
}
